package com.dy.分类.哈希表和字符串._71_简化路径;

import java.util.Stack;

public class Solution {
    static public String simplifyPath(String path) {
        Stack<String> stack = new Stack<>();
        String[] s = path.split("/");

        for(int i=0;i<s.length;i++){
            if(s[i].length()==0) continue;
            if(s[i].equals(".")) continue;
            if(s[i].equals("..") ){
                if(!stack.isEmpty()) {
                    stack.pop();
                }
            }else{
                stack.push(s[i]);
            }
        }

        StringBuilder res = new StringBuilder("");
        res.append("/");
        for(int i=0;i<stack.size();i++){
            res.append(stack.elementAt(i));
            if(i<stack.size()-1) {
                res.append("/");
            }
        }
        return res.toString();
    }

    public static void main(String[] args) {
        System.out.println(simplifyPath("/../"));
    }
}
